// pages/my/updateUser/updateUser.js
import {
  updatePassword,
  updateNick
} from "../../../api/index"

Page({

  /**
   * 页面的初始数据
   */
  data: {
    // 1.修改密码 2.修改昵称
    type: '1',
    oldPass: '',
    newPass: '',
    nickName: ''
  },
  onLoad(play) {
    console.log(play.type);
    this.setData({
      type: play.type
    })
  },
  //更改用户
  async updateUser() {
    try {

      if (this.data.type == '1') {
        const old_password = this.data.oldPass
        const new_password = this.data.newPass
        const dataObj = await await updatePassword({
          old_password,
          new_password
        })
        console.log("密码修改", dataObj);
        if (dataObj.code == 200) {
          wx.showToast({
            title: '修改成功',
          })

          setTimeout(() => {
            wx.removeStorageSync('userInfo')

            wx.reLaunch({
              url: '/pages/login/login'
            })
          }, 1000);
        }
      } else {
        const name = this.data.nickName
        const dataObj = await updateNick({
          name
        })
        console.log("用户名修改", dataObj);
        if (dataObj.code == 200) {
          wx.showToast({
            title: '修改成功',
          })

          setTimeout(() => {
            wx.navigateBack(-1)
          }, 1000);
        }
      }
    } catch (error) {
      console.log("修改用户信息错误：", error);
    }

  }

})